package com.neusoft.elm.application.service;

import com.neusoft.elm.domain.entity.User;
import com.neusoft.elm.domain.repository.UserRepository;
import com.neusoft.elm.domain.service.UserService;
import com.neusoft.elm.infrastructure.exception.BusinessException;
import com.neusoft.elm.infrastructure.persistence.UserRepositoryImpl;

public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;

    public UserServiceImpl() {
        this.userRepository = new UserRepositoryImpl();
    }

    @Override
    public User login(String userId, String password) {
        User user = userRepository.findByUserIdAndPassword(userId, password);
        if (user == null) {
            throw new BusinessException("用户名或密码错误");
        }
        return user;
    }

    @Override
    public int register(User user) {
        if (checkUserId(user.getUserId())) {
            throw new BusinessException("用户ID已存在");
        }
        return userRepository.save(user);
    }

    @Override
    public boolean checkUserId(String userId) {
        return userRepository.countByUserId(userId) > 0;
    }
} 